﻿using FlyHorse.LIMS.Common;
using FlyHorse.LIMS.IBLL;
using FlyHorse.LIMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FlyHorse.LIMS.BLL
{
    public partial class CustomerBll : BaseBll<Customer>, ICustomerBll
    {
        public bool AddCustomerAndStandard(Customer entity)
        {
            using (OJCB4Entities db = new OJCB4Entities())
            {
                Customer customer = db.Customers.Add(entity);
                //查询有没有标准
                int count = this.CurrentDBSession.StandardCustomerDal.LoadEntitiesAsNoTracking(m => m.CustomerID == customer.CustomerID).Count();
                if (count == 0)
                {
                    SysReportType reportType = this.CurrentDBSession.SysReportTypeDal.LoadEntitiesAsNoTracking(m => m.SysReportTypeID == "3").First();
                    List<Good> goodsList = this.CurrentDBSession.GoodDal.LoadEntitiesAsNoTracking(m => m.GoodsType.GoodsTypeName == "产品").ToList();
                    List<StandardCustomer> standardList = new List<StandardCustomer>();
                    foreach (var goods in goodsList)
                    {
                        foreach (var item in reportType.TestItems)
                        {
                            StandardCustomer standard = new StandardCustomer();
                            standard.StandardCustomerID = Guid.NewGuid().ToString();
                            standard.CustomerID = customer.CustomerID;
                            standard.FormOrder = item.Order;
                            standard.GoodsID = goods.GoodsID;
                            standard.MaxValue = null;
                            standard.MinValue = null;
                            standard.TestItemID = item.TestItemID;
                            standardList.Add(standard);
                        }
                    }
                    foreach (var standard in standardList)
                    {
                        db.Set<StandardCustomer>().Add(standard);
                    }
                }

                int res = db.SaveChanges();
                if (res > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public bool DeleteCustomerAndstandard(Customer entity)
        {
            //db.Entry(info).State = System.Data.EntityState.Modified;  
            using (OJCB4Entities db = new OJCB4Entities())
            {
                List<StandardCustomer> standardList = db.StandardCustomers.Where(m => m.CustomerID == entity.CustomerID).ToList();
                var model = db.Customers.Find(entity.CustomerID);
                db.Set<Customer>().Remove(model);
                foreach (var standard in standardList)
                {
                    db.Set<StandardCustomer>().Remove(standard);
                }
                int count = db.SaveChanges();
                if (count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
}
